﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using ProcessControl;
using MyLib;
using System.Data;

namespace OrderContract
{
    public partial class NewRequest : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

            //((ButtonList)ButtonList1).BeforeSubmit += new System.ComponentModel.CancelEventHandler(NewRequest_BeforeSubmit);
            //if (!IsPostBack)
            //{
            //    if (Convert.ToInt32(Request.QueryString["Incident"].ToString()) > 100000 && Convert.ToInt32(Request.QueryString["Incident"].ToString()) != 0 && fld_TCONTRACTTYPE.SelectedIndex!=0)
            //    {
            //        GetYWLX();
            //    }
            //}
        }
        //
        void NewRequest_BeforeSubmit(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                string useraccount = Page.User.Identity.Name.Split('\\')[1].ToString();
                string contractNo = "";
                DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("select * from V_EMPDEPARTMENTS t  where employeeid='" + useraccount + "'");
                if (dt.Rows.Count > 0)
                {
                    string departmentid = dt.Rows[0]["departmentid"].ToString();
                    do
                    {
                        DataTable dt1 = DataAccess.Instance("BizDB").ExecuteDataTable("select * from  v_department where departmentid='" + departmentid + "'");
                        if (dt1.Rows.Count > 0)
                        {
                            string cocentre = dt1.Rows[0]["departmentname"].ToString();
                            departmentid = dt1.Rows[0]["parentid"].ToString();
                            if (dt1.Rows[0]["parentid"].ToString() != dt1.Rows[0]["departmentid"].ToString())
                            {
                                switch (cocentre.Substring(0,cocentre.IndexOf('[')))
                                {
                                    case "服务中心":
                                        contractNo = "FW";
                                        break;
                                    case "咨询中心":
                                        contractNo = "ZX";
                                        break;
                                    case "软件中心":
                                        contractNo = "RJ";
                                        break;
                                    case "工程中心":
                                        contractNo = "GC";
                                        break;
                                    case "技术中心":
                                        contractNo = "JS";
                                        break;
                                    case "运营管理中心":
                                        contractNo = "YY";
                                        break;
                                    case "国际服务部":
                                        contractNo = "GJ";
                                        break;
                                    case "采购部":
                                        contractNo = "CG";
                                        break;
                                    default:
                                        contractNo = "";
                                        break;
                                }
                            }
                            else
                            {
                                contractNo = "NO";
                            }
                        }
                    else
                    {
                    contractNo = "NO";
                    }
                    }
                    while (contractNo == "");
                }
                if (contractNo != "NO")
                {
                  string sszt=((DropDownList)UserInfo1.FindControl("fld_COMPANY")).SelectedItem.Text;
                // fld_TCONTRACTNO.Text = GetTBELONGMAIN(sszt) + "-" + contractNo + "-" + GetTBUSSNIESSTYPE(fld_TBUSSNIESSTYPE.SelectedItem.Text) + "-" + (DateTime.Now.Year - 2000).ToString();
                }
            }
            catch (Exception ex)
            {
                throw new NotImplementedException();
            }
        }

        //获得主体代码
        public string GetTBELONGMAIN(string fld_TBELONGMAIN)
        { 
            string str="";
          switch(fld_TBELONGMAIN)
            {
                  case"新疆金风科技股份有限公司":
                  str="BJTYS";
                  break;
                  case"北京金风科创风电设备有限公司":
                  str="BJKCS";
                  break;
                  case"北京天源科创风电技术有限责任公司":
                  str="XJGWS";
                  break;
                  case"软件公司":
                  str="RJGSS";
                  break;
                  default :
                  break;
            }
          return str;
        }

        public string GetTBUSSNIESSTYPE(string fld_TBUSSNIESSTYPE)
        { 
         string str="";
         switch (fld_TBUSSNIESSTYPE)
         {
                 case"设备物资":
                 str="SBWZ";
                 break;
                 case"材料销售":
                 str="CLXS";
                 break;
                   case"产品销售":
                 str="CPXS";
                 break;
                   case"测风咨询":
                 str="CFZX";
                 break;
                   case"硬件销售":
                 str="YJXS";
                 break;
                   case"软件销售":
                 str="RJXS";
                 break;
                   case"协议订单":
                 str="XYDD";
                 break;
             case"技术服务合同":
                 str="JSFW";
                 break;
             default:
                 break;
         }
         return str;
        }

        //protected void fld_TCONTRACTTYPE_SelectedIndexChanged(object sender, EventArgs e)
        //{
           
        //    if (fld_TCONTRACTTYPE.SelectedItem.Text == "销售制式合同及订单")
        //    {
        //        GetYWLX();
        //    }
        //    else
        //    {
        //        GetBussniessType("无");
        //    }
        //}

        public void GetYWLX()
        {
            string type1 = "材料销售,产品销售,技术服务"; //服务中心、技术中心、国际服务部
            string type2 = "设备物资,技术服务"; //工程中心
            string type3 = "技术服务,测风咨询"; //咨询中心
            string type4 = "硬件销售,软件销售,技术服务"; //软件中心
            string type5 = "无";
            string useraccount = Page.User.Identity.Name.Split('\\')[1].ToString();
            string contractNo = "";
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("select * from V_EMPDEPARTMENTS t  where employeeid='" + useraccount + "'");
            if (dt.Rows.Count > 0)
            {
                string departmentid = dt.Rows[0]["departmentid"].ToString();
                do
                {
                    DataTable dt1 = DataAccess.Instance("BizDB").ExecuteDataTable("select * from  v_department where departmentid='" + departmentid + "'");
                    if (dt1.Rows.Count > 0)
                    {
                        string cocentre = dt1.Rows[0]["departmentname"].ToString();
                        departmentid = dt1.Rows[0]["parentid"].ToString();
                        if (dt1.Rows[0]["parentid"].ToString() != dt1.Rows[0]["departmentid"].ToString())
                        {
                            switch (cocentre.Substring(0, cocentre.IndexOf('[')))
                            {
                                case "服务中心":
                                    contractNo = "Yes";
                                    GetBussniessType(type1);
                                    break;
                                case "技术中心":
                                    contractNo = "Yes";
                                    GetBussniessType(type1);
                                    break;
                                case "国际服务部":
                                    contractNo = "Yes";
                                    GetBussniessType(type1);
                                    break;
                                case "工程中心":
                                    contractNo = "Yes";
                                    GetBussniessType(type2);
                                    break;
                                case "咨询中心":
                                    contractNo = "Yes";
                                    GetBussniessType(type3);
                                    break;
                                case "软件中心":
                                    contractNo = "Yes";
                                    GetBussniessType(type4);
                                    break;
                                default:
                                    GetBussniessType(type5);
                                    break;
                            }
                        }
                        else
                        {
                            contractNo = "No";
                            GetBussniessType(type5);
                        }
                    }
                    else
                    {
                        contractNo = "No";
                        GetBussniessType(type5);
                    }
                }
                while (contractNo == "");
            }
        }

        public void GetBussniessType(string data)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < data.Split(',').Length; i++)
            {
                list.Add(data.Split(',')[i].ToString());
            }
            //fld_TBUSSNIESSTYPE.DataSource = list;
            //fld_TBUSSNIESSTYPE.DataBind();
        }
        
    }
}